package com.darker.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.darker.reggie.common.CustomException;
import com.darker.reggie.dto.SetmealDto;
import com.darker.reggie.entity.Setmeal;
import com.darker.reggie.entity.SetmealDish;
import com.darker.reggie.mapper.SetmealMapper;
import com.darker.reggie.service.SetmealDishService;
import com.darker.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Resource
    SetmealDishService setmealDishService;


    //保存套餐信息和套餐与菜品的关系
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的信息
        this.save(setmealDto);

        //保存套餐与菜品的关系信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }
    @Transactional
    @Override
    public void delById(List<Long> ids) {
        for(long id:ids){
            //查询删除的套餐是否在售
            LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getStatus,1);
            queryWrapper.in(Setmeal::getId,id);
            int count = this.count(queryWrapper);
            //如果在售  抛出异常信息
            if(count>0){
                throw  new CustomException("当前套餐正在售卖 不能删除");
            }


            //删除套餐信息
            this.removeByIds(ids);


            //删除套餐与菜品的对应关系
            LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
            wrapper.in(SetmealDish::getSetmealId,ids);
            setmealDishService.remove(wrapper);
        }
    }

    @Override
    public SetmealDto getSetmealDto(long id) {
        SetmealDto setmealDto=new SetmealDto();
        //把查到的套餐信息赋值给setmealDto
        Setmeal setmeal = this.getById(id);//查询菜品信息
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询菜品与套餐的关系信息
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(lambdaQueryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;


    }

    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //删除套餐信息
        this.removeById(setmealDto.getId());
        //删除套餐与菜品关系信息
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //再调用新增方法 添加数据
        this.saveWithDish(setmealDto);
    }




}
